<template>
  <div>
    <!-- 搜索框 -->
  <van-search
    v-model.trim="value"
    shape="round"
    placeholder="请输入搜索关键词"
    @input="onInput"
    />
    <!-- 热门搜索 -->
    <div class="hotSearch" v-if="!searchList.length">
      <p class="hotTit">热门搜索</p>
       <div class="hot-tags">
         <span @click="onClickTags(item.first)" v-for="item in hotSearch" :key="item.first"> {{ item.first}} </span>
       </div>
    </div>
    <!-- 搜索结果 -->
    <div class="searchRes" v-else>
      <p class="searchTit">最佳匹配</p>

      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <MusicItem
      :name="item.name"
      :authorName="item.ar[0].name"
      :id="item.id"
      v-for="(item,index) in searchList"
      :key="index"/>
      </van-list>
    </div>
  </div>
</template>

<script>
import { getHotSearch, getSearchRes } from '@/api/Search.js'
import MusicItem from '@/components/MusicItem.vue'
export default {
  name: 'search',
  data () {
    return {
      value: '',
      hotSearch: [],
      searchList: [],
      loading: false, // 是否显示加载loading
      finished: false, // 是否全部加载完成 如果为true 会显示没有数据的文本,不会加载下一项
      offset: 0,
      page: 28

    }
  },
  components: {
    MusicItem
  },
  created () {
    this.loadHotSearch()
  },

  methods: {
    // 加载热门搜索数据
    async loadHotSearch () {
      const res = await getHotSearch()
      this.hotSearch = res.data.result.hots
    },
    // 点击了每一项的tags
    async onClickTags (name) {
      this.value = name
      // 获取搜索结果
      const res = await getSearchRes({
        keywords: this.value,
        limit: 10
      })
      // console.log(res)
      this.searchList = res.data.result.songs
    },
    // 输入框的变化
    onInput () {
      this.page = 1
      if (this.value === '') {
        this.searchList = []
        clearTimeout(this.id)
        return
      }
      // 添加防抖
      if (this.id) {
        clearTimeout(this.id)
      }
      this.id = setTimeout(async () => {
        const res = await getSearchRes({
          keywords: this.value,
          limit: 10
        })
        this.searchList = res.data.result.songs
      }, 800)
    },
    // 加载更多模块
    async  onLoad () {
      this.page++
      // 1.页面进入
      // 2.数据没有占满一屏
      // 3.触底
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      // 发送请求

      console.log('触发load')
      const res = await getSearchRes({
        keywords: this.value,
        limit: 10,
        offset: (this.page - 1) * 10
      })
      // 保存数据
      if (res.data.result.songs) {
        this.searchList.push(...res.data.result.songs)
        // 关闭loading
        this.loading = false
      } else {
        this.finished = true
      }
    }
    // 判断是否加载下一页的数据
    // 所有数据加载了   不去加载下一页 finished true
    //  数据没有完成 加载下一页 啥也不干
  }

}

</script>

<style scoped>
.hotSearch .hotTit {
  font-size: 13px;
  color: #053a12;
  margin-left: 10px;
  padding: 0 10px;
}
.hotSearch .hot-tags span {
  display: inline-block;
  font-size: 13px;
  border: 1px solid #308f3d;
  border-radius: 15px;
  padding: 5px 10px;
  color: #5e5b5b;
  margin: 5px 0;
  margin-right: 8px;
}
.searchRes {
  padding: 0 10px;
}
.searchRes .searchTit {
  font-size: 13px;
  color: #053a12;
}
</style>
